﻿using cap01.Common.Enums;
using cap01.Common.Util;
using DotNetCore.CAP;

namespace cap01.Application.Cap;

/// <summary>
/// CAP
/// </summary>
[ApiRoute.App]
public class CapController(ICapPublisher capBus) : ControllerBase
{
    private readonly ICapPublisher capBus = capBus;

    /// <summary>
    /// 发送CAP消息
    /// </summary>
    [HttpGet("send")]
    public async Task SendMessage()
    {
        await capBus.PublishAsync("TestCap", DateTimeOffset.Now, new Dictionary<string, string>
        {
            { "name", "孙小双" },
            { "age", "29" }
        });
    }

    /// <summary>
    /// 接收CAP消息
    /// </summary>
    /// <param name="dateTime"></param>
    /// <param name="header"></param>
    [NonAction]
    [CapSubscribe("TestCap")]
    public void ReceiveMessage(DateTimeOffset dateTime, [FromCap] CapHeader header)
    {
        Console.WriteLine("收到消息：" + dateTime + "，header：" + header.ToJson());
    }
}
